/*--------------------大数据管理中心.首页设置--ly-----------------*/
/*---------------------首页数据列表 index_title_list.html ------------------*/

var tableurl = dataport + "/LabourController/findByPage"; //get/post请求数据url 
var teamurl = dataport + "/LabourTeamController/selectById";
var joburl = dataport + "/LabourJobController/selectById";
//页面table渲染
$(document).ready(function() {
	$("#tablebody").find("tr").remove(); //清空表格数据
	tableget();
});

//第一次table数据请求
var tableget = (function() {
	$("#tablebody").find("tr").remove(); //清空表格数据 
	var size = $("#pageSize").val();
	//选择分页请求
	$.post(tableurl, {
			'pageNo': 1,
			'pageSize': size
		},
		function(result) {
			var code = result.code;
			var list = result.object;
			if(code == 0) {
				var ant = list.total;
				antrows = ant; //将查询结果总数变成全局变量
				$('.totalNum').text(ant);
				$.each(list.list, function(idx, obj) {
					//					console.log(result);
					var idx = idx + 1; //排序默认从0开始，idx将作为显示题号，因此+1
					var companyid = obj.companyid;
					var jobid = obj.jobid;
					var teamid = obj.teamid;
					var Qhtml =
						'<tr id="' + obj.labourid + '">\
          <td>' + idx + '</td>\
          <td>' + obj.name + '</td>\
          <td>' + obj.gender + '</td>\
          <td>' + obj.birthday + '</td>\
          <td>2017-01-01 11:11:42</td>\
           <td class="td-status">\
            <span class="layui-btn layui-btn-normal layui-btn-mini lay' + obj.state + '"></span>\
          </td>\
          <td class="td-manage">\
          		<a class="' + idx + ' lay' + obj.state + '" onclick="member_stop(this,10001,' + idx + ',' + obj.state + ')" href="javascript:;" title="启用">\
              <i class="layui-icon lay' + obj.state + '">&#xe601;</i>\
            </a>\
               <a title="修改基础信息" class="edit' + idx + '"  onclick="x_admin_show(title,peopleEdit,50,82,' + idx + ')" href="javascript:;">\
                <i class="layui-icon">&#xe642;</i>\
              </a>\
             <a title="编辑职能信息"  onclick="x_admin_show(title,peoplefunctionEdit,40,62,' + idx + ')" href="javascript:;">\
                <i class="layui-icon">&#xe63c;</i>\
              </a>\
            </td>\
        </tr>';

					$("#tablebody").append(Qhtml);
					$(".td-manage .lay1").attr('title', '停用').find('i').html('&#xe601');
					$(".td-manage .lay0").attr('title', '启用').find('i').html('&#xe62f;');
					$(".td-status .lay1").text("已启用").removeClass('layui-btn-disabled');
					$(".td-status .lay0").text("已停用").addClass('layui-btn-disabled');
					getjobname(jobid);
					getteamname(teamid);
				});
				pagerNum();
			} else {
				alert('您的登录认证已失效，请重新登录！');
			};
		}, 'JSON');
});


var getjobname = (function(jobid) {
	$.post(joburl, {
		jobId: jobid,
	}, function(result) {
		var name = result.object;
		if(result.code == 0) {
			$("." + jobid).text(name.jobname);
		} else {
			$("." + jobid).text("未知");
		}
	});
});

var getteamname = (function(teamid) {
	$.post(teamurl, {
		teamId: teamid,
	}, function(result) {
		var team = result.object;
		if(result.code == 0) {
			$("." + teamid).text(team.teamname);
		} else {
			$("." + teamid).text("未进组");
		}
	});
});

//分页跳转--------修改行数()-----------
var rowData = (function() {
	$("#pageSize").blur(function() {
		size = $("#pageSize").val();
		pagerNum(size);
	})
});

//翻页事件
var pagerNum = (function() {
	var pageSizeNum = $("#pageSize").val();
	var antrows = $('.totalNum').text();
	var pager = jQuery('#ampagination-libChoice').pagination({
		page: 1, //默认页码
		totals: antrows,
		pageSize: pageSizeNum,
		theme: 'amazeui'
	});
	sreach();
	//分页跳转-------修改页码--------
	pager.onChangePage(function(e) {
		jQuery('.showlabel').text('The selected page no: ' + e.page);
		var no = e.page; //页号
		var size = $("#pageSize").val();
		//选择分页请求
		$("#tablebody").find("tr").remove();
		$.post(tableurl, {
			pageNo: no,
			pageSize: size,
		}, function(result) {
			var code = result.code;
			var list = result.object;
			if(code == 0) {
				$.each(list.list, function(idx, obj) {
					var idx = idx + 1; //排序默认从0开始，idx将作为显示题号，因此+1
					var id = obj.labourid;
					var companyid = obj.companyid;
					var jobid = obj.jobid;
					var teamid = obj.teamid;
					var Qhtml =
						'<tr id="' + obj.labourid + '">\
          <td>' + idx + '</td>\
          <td>' + obj.name + '</td>\
          <td>' + obj.gender + '</td>\
          <td>' + obj.birthday + '</td>\
          <td>2017-01-01 11:11:42</td>\
           <td class="td-status">\
            <span class="layui-btn layui-btn-normal layui-btn-mini lay' + obj.state + '"></span>\
          </td>\
          <td class="td-manage">\
          		<a class="' + idx + ' lay' + obj.state + '" onclick="member_stop(this,10001,' + idx + ',' + obj.state + ')" href="javascript:;" title="启用">\
              <i class="layui-icon lay' + obj.state + '">&#xe601;</i>\
            </a>\
               <a title="修改基础信息" class="edit' + idx + '"  onclick="x_admin_show(title,peopleEdit,50,82,' + idx + ')" href="javascript:;">\
                <i class="layui-icon">&#xe642;</i>\
              </a>\
             <a title="编辑职能信息"  onclick="x_admin_show(title,peoplefunctionEdit,40,62,' + idx + ')" href="javascript:;">\
                <i class="layui-icon">&#xe63c;</i>\
              </a>\
            </td>\
        </tr>';
					$("#tablebody").append(Qhtml);
					$("#rowData").css("pointer-events", "none");
					$(".td-manage .lay1").attr('title', '停用').find('i').html('&#xe601');
					$(".td-manage .lay0").attr('title', '启用').find('i').html('&#xe62f;');
					$(".td-status .lay1").text("已启用").removeClass('layui-btn-disabled');
					$(".td-status .lay0").text("已停用").addClass('layui-btn-disabled');
					getteamname(teamid);
					getcompanyname(companyid);
				});
			} else {
				alert('error');
			};
		}, 'JSON');
	});
})

/*用户-停用*/
function member_stop(obj, id, idx, state) {
	var labourid = $("." + idx).parents("tr").attr("id");
	var stopurl = dataport + '/LabourController/update';
	if(state == 1) {
		state = state - 1;
	} else {
		state = state + 1;
	}
	$.post(stopurl, {
		'labourid': labourid,
		'state': state
	}, function(result) {
		console.log(result);
		var code = result.code;
		if(code == 0) {
			if($(obj).attr('title') == '启用') {
				layer.confirm('确认要启用吗？', function(index) {
					$(obj).attr('title', '启用');
					$(obj).find('i').html('&#xe62f;');
					$(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
					layer.msg('已启用!', {
						icon: 6,
						time: 1000
					});
				})

			} else {
				layer.confirm('确认要停用吗？', function(index) {
					$(obj).attr('title', '停用')
					$(obj).find('i').html('&#xe601;');
					$(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
					layer.msg('已停用!', {
						icon: 5,
						time: 1000
					});
				})
			}
		} else {
			layer.msg('网络异常,稍后重试!', {
				icon: 5,
				time: 1000
			})
		}
	})
};

/*模糊查询 --------*/
var sreach = (function() {
	$("#sreach").click(function() {
		var name = $("#searchText").val();
		console.log(name);
		var sreachurl = dataport + "/LabourController/findByLikePage";
		$("#tablebody").find("tr").remove(); //清空表格数据
		$.post(sreachurl, {
			name: name,
			pageNo: '1',
			pageSize: '99'
		}, function(result) {
			console.log(result);
			var code = result.code;
			var list = result.object;
			var ant = list.total;
			$('.totalNum').text(ant);
			if(code == 0) {
				var ant = list.total;
				antrows = ant; //将查询结果总数变成全局变量
				$('.totalNum').text(ant);
				$.each(list.list, function(idx, obj) {
					var idx = idx + 1; //排序默认从0开始，idx将作为显示题号，因此+1
					var companyid = obj.companyid;
					var jobid = obj.jobid;
					var teamid = obj.teamid;
					var Qhtml =
						'<tr id="' + obj.labourid + '">\
          <td>' + idx + '</td>\
          <td>' + obj.name + '</td>\
          <td>' + obj.gender + '</td>\
           <td>' + obj.birthday + '</td>\
          <td>2017-01-01 11:11:42</td>\
          <td class="td-status">\
            <span class="layui-btn layui-btn-normal layui-btn-mini lay' + obj.state + '">已启用</span>\
          </td>\
          <td class="td-manage">\
          		<a class="' + idx + ' lay' + obj.state + '" onclick="member_stop(this,10001,' + idx + ',' + obj.state + ')" href="javascript:;" title="">\
              <i class="layui-icon">&#xe601;</i>\
            </a>\
              <a title="修改基础信息" class="edit' + idx + '"  onclick="x_admin_show(title,peopleEdit,40,82,' + idx + ')" href="javascript:;">\
                <i class="layui-icon">&#xe642;</i>\
              </a>\
              <a title="编辑职能信息"  onclick="x_admin_show(title,peoplefunctionEdit,40,62,' + idx + ')" href="javascript:;">\
                <i class="layui-icon">&#xe63c;</i>\
              </a>\
            </td>\
        </tr>';

					$("#tablebody").append(Qhtml);
					$(".pagelist").css("display","none");
					$(".td-manage .lay1").attr('title', '停用').find('i').html('&#xe601');
					$(".td-manage .lay0").attr('title', '启用').find('i').html('&#xe62f;');
					$(".td-status .lay1").text("已启用").removeClass('layui-btn-disabled');
					$(".td-status .lay0").text("已停用").addClass('layui-btn-disabled');
					getcompanyname(companyid);
					getjobname(jobid);
					getteamname(teamid);
				});
			} else {
				alert('您的登录认证已失效，请重新登录！');
			};
		})
	})
});

//修改功能--跨页存储
var editsave = (function(classid) {
	var labourid = $("." + classid).parents("tr").attr("id");
	$.cookie("labourid", labourid);
})

//一键删除
function delAll(argument) {
	var data = tableCheck.getData();
	layer.confirm('确认要删除吗？' + data, function(index) {
		//捉到所有被选中的，发异步进行删除
		layer.msg('删除成功', {
			icon: 1
		});
		$(".layui-form-checked").not('.header').parents('tr').remove();
	});
}